package authoritah.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class Item {
	private int id;
	private String name;
	
	/////////////////////////
	// Getters and Setters //
	/////////////////////////
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	//////////////////////
	// Database methods //
	//////////////////////
	
	/**
	 * Retrieves a member from the database file. If no member with supplied id exists, returns null.
	 * @param id
	 * @return
	 */
	public static Item getItem(int id){
		Item item = new Item();
		try {
            ParityDB db = ParityDB.getInstance();
            
            Connection conn = db.getConn();
            
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM item WHERE id=?");
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            
            // Only interested in first result as id is primary key should never have more than result anyway.
        	rs.next();            	
            item.setId(rs.getInt("id"));
            item.setName(rs.getString("name"));
            
            // Close the connection
            conn.close();
            
        }
        catch (Exception e) {
        	e.printStackTrace();
        }
        return item;
	}
	
	/**
	 * Retrieves a member from the database file. If no member with supplied name exists, returns null.
	 * @param name
	 * @return
	 */
	public static Item getItem(String name){
		Item item = new Item();
		try {
            ParityDB db = ParityDB.getInstance();
            
            Connection conn = db.getConn();
            
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM item WHERE name=?");
            ps.setString(1, name);
            ResultSet rs = ps.executeQuery();
            
            // Only interested in first result as id is primary key should never have more than result anyway.
        	rs.next();            	
            item.setId(rs.getInt("id"));
            item.setName(rs.getString("name"));
            
            // Close the connection
            conn.close();
            
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return item;
	}
	
	/**
	 * Stores a member into the database.
	 * @param item
	 */
	public static void createItem(Item item){
		try {
			ParityDB db = ParityDB.getInstance();            
            Connection conn = db.getConn();
            
            PreparedStatement ps = conn.prepareStatement("INSERT INTO item (name) VALUES (?)");
            ps.setString(1, item.getName());
            ps.executeUpdate();
            
            // Close the connection
            conn.close();
            
        }
        catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	/**
	 * Retrieves all members in the database.
	 * @return All members currently in the database.
	 */
	public static ArrayList<Item> getAllItems() {
		ArrayList<Item> items = new ArrayList<Item>();
		try {
			ParityDB db = ParityDB.getInstance();            
            Connection conn = db.getConn();
            
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM item");
            ResultSet rs = ps.executeQuery();
            
            // Iterate the result set, adding to the collection.
            while (rs.next()) {
            	Item item = new Item();
                item.setId(rs.getInt("id"));
                item.setName(rs.getString("name"));
                items.add(item);
            }
            
            // Close the connection
            conn.close();
            
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return items;
	}
	public static void updateItem(Item item) {
		try {
			ParityDB db = ParityDB.getInstance();            
            Connection conn = db.getConn();
            
            PreparedStatement ps = conn.prepareStatement("UPDATE item SET name = ? WHERE id = ?");
            ps.setString(1, item.getName());
            ps.setInt(2, item.getId());
            ps.executeUpdate();
            
            // Close the connection
            conn.close();
            
        }
        catch (Exception e) {
            e.printStackTrace();
        }		
	}
	public static void removeItem(Item item) {
		try {
			ParityDB db = ParityDB.getInstance();            
            Connection conn = db.getConn();
            
            PreparedStatement ps = conn.prepareStatement("DELETE FROM item WHERE id = ?");
            ps.setInt(1, item.getId());
            ps.executeUpdate();
            
            // Close the connection
            conn.close();
            
        }
        catch (Exception e) {
            e.printStackTrace();
        }	
	}
	
	public String toString() {
		return name;
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Item other = (Item) obj;
		if (id != other.id)
			return false;
		return true;
	}
	
	
}
